package datastruct

type LinkNode struct {
	Data int64
	Next *LinkNode
}

type RingNode struct {
	Data       int64
	Next, Prev *RingNode
}

func (r *RingNode) Init() *RingNode {
	r.Next = r
	r.Prev = r
	return r
}

func NewRing(n int) *RingNode {
	if n <= 0 {
		return nil
	}
	r := new(RingNode)
	p := r
	for i := 0; i < n; i++ {
		p.Next = &RingNode{Prev: p}
		p = p.Next
	}
	p.Next = r
	r.Prev = p
	return r
}

func (r *RingNode) NextNode() *RingNode {
	if r.Next == nil {
		return r.Init()
	}
	return r.Next
}
func (r *RingNode) PrevNode() *RingNode {
	if r.Prev == nil {
		return r.Init()
	}
	return r.Prev
}
func (r *RingNode) Move(n int) *RingNode {
	if r.Next == nil {
		return r.Init()
	}
	switch {
	case n < 0:
		for ; n < 0; n++ {
			r = r.Prev
		}
	case n > 0:
		for ; n > 0; n-- {
			r = r.Next
		}
	}
	return r
}
